package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.AgricultureGroomListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.AgricultureGroom;
import com.isunland.managesystem.entity.AgricultureGroomOriginal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/5 15:15
 */
@SuppressWarnings("deprecation") public class AgricultureGroomListFragment
    extends BaseListFragment {
  public static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.FinanceFindQueryFragment.EXTRA_TYPE";
  public static final String AGRICULTURAL_PRODUCTION = "11";//农资交易中心
  public static final String AGRICULTURAL_VISIT = "22";//观光农业认养
  private final int REQUEST_QUERY = 1;

  private int mPage = 1;
  private String groomtype;
  private String beginDate;
  private String endDate;
  private String groomplatform;
  private ArrayList<AgricultureGroom> mList;
  private AgricultureGroomListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    groomtype = getArguments().getString(EXTRA_TYPE);
    if (AGRICULTURAL_PRODUCTION.equalsIgnoreCase(groomtype)) {
      setTitleCustom("农资交易中心");
    }
    if (AGRICULTURAL_VISIT.equalsIgnoreCase(groomtype)) {
      setTitleCustom("观光农业认养");
    }
    mList = new ArrayList<>();
    beginDate = MyDateUtil.parseDateToString(MyDateUtil.getBeforeMonth(3));
    endDate = MyDateUtil.parseDateToString(new Date());
    groomplatform = "";
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      //查询
      case R.id.menu_item_query:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(beginDate);
        queryConten.setExtName2(endDate);
        queryConten.setExtName3(groomplatform);
        Intent query = new Intent(getActivity(), AgricultureGroomQueryActivity.class);
        query.putExtra(AgricultureGroomQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_AGRICULTURAL_GROOM;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("page", mPage + "");
    params.put("pageSize", "20");
    params.put("beginregDate", beginDate);
    params.put("endregDate", endDate);
    params.put("groomtype", groomtype);
    params.put("groomplatform", groomplatform);
    return params;
  }

  @Override public void resolveResponse(String json) {
    AgricultureGroomOriginal result = new Gson().fromJson(json, AgricultureGroomOriginal.class);
    ArrayList<AgricultureGroom> rows = result.getRows();
    if (Base.SUCCESS == result.getResult() && result.getRows() != null) {
      if (rows != null && rows.size() == 0) {
        ToastUtil.show(R.string.that_is_all);
      }

      //获取列表成功
      if (mPage == 1) {
        mList.clear();
      }
      mList.addAll(result.getRows());
      if (mAdapter == null) {
        mAdapter = new AgricultureGroomListAdapter<>(getActivity(), mList, groomtype);
        setListAdapter(mAdapter);
        return;
      }
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    AgricultureGroom content = mList.get(position - 1);
    Intent i = new Intent(getActivity(), AgricultureGroomDetailActivity.class);
    i.putExtra(AgricultureGroomDetailFragment.EXTRA_CONTENT, content);
    startActivity(i);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    if (PullToRefreshBase.Mode.PULL_FROM_START == refreshView.getCurrentMode()) {
      mPage = 1;
    }
    if (PullToRefreshBase.Mode.PULL_FROM_END == refreshView.getCurrentMode()) {
      mPage++;
    }
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {

      ExtraValueContent queryConten = (ExtraValueContent) data.getSerializableExtra(
          AgricultureGroomQueryFragment.EXTRA_CONTENT);
      beginDate = queryConten.getExtName1();
      endDate = queryConten.getExtName2();
      groomplatform = queryConten.getExtName3();

      mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
      mPage = 1;
      volleyPost();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static AgricultureGroomListFragment newInstance(String groomtype) {

    Bundle args = new Bundle();
    args.putString(EXTRA_TYPE, groomtype);
    AgricultureGroomListFragment fragment = new AgricultureGroomListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
